//
//  UIViewController+XLExtension.m
//  DrugProphet
//
//  Created by 左学良 on 16/7/21.
//  Copyright © 2016年 左学良. All rights reserved.
//

#import "UIViewController+XLExtension.h"
#import "UIBarButtonItem+XLExtension.h"
#define itemColor [UIColor whiteColor]
@implementation UIViewController (XLExtension)

- (void)buildNavLeftItemsWithImgName:(NSString *)imgName action:(SEL)action secondImgName:(NSString *)secondImgName secondAction:(SEL)secondAction
{
    
}

- (void)buildNavRightItemsWithImgName:(NSString *)imgName action:(SEL)action tag:(NSInteger)tag secondImgName:(NSString *)secondImgName secondAction:(SEL)secondAction
{
    UIBarButtonItem *rightItem1 =[UIBarButtonItem itemWithImageName:imgName highImageName:imgName target:self action:action];
    UIButton *btn = (UIButton *)rightItem1.customView;
    btn.tag = tag;
    
    UIBarButtonItem *rightItem2 =[UIBarButtonItem itemWithImageName:secondImgName highImageName:secondImgName target:self action:secondAction];
    
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    negativeSpacer.width = -8;
    
    UIBarButtonItem *negativeSpacer1 = [[UIBarButtonItem alloc]
                                        initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                        target:nil action:nil];
    negativeSpacer1.width = 15;
    
    self.navigationItem.rightBarButtonItems = @[negativeSpacer,rightItem1,negativeSpacer1,rightItem2];
}

- (void)buildNavRightItemsWithImgName:(NSString *)imgName action:(SEL)action secondImgName:(NSString *)secondImgName secondAction:(SEL)secondAction
{
    UIBarButtonItem *rightItem1 =[UIBarButtonItem itemWithImageName:imgName highImageName:imgName target:self action:action];
    
    UIBarButtonItem *rightItem2 =[UIBarButtonItem itemWithImageName:secondImgName highImageName:secondImgName target:self action:secondAction];
    
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    negativeSpacer.width = -8;
    
    UIBarButtonItem *negativeSpacer1 = [[UIBarButtonItem alloc]
                                        initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                        target:nil action:nil];
    negativeSpacer1.width = 15;
    
    self.navigationItem.rightBarButtonItems = @[negativeSpacer,rightItem1,negativeSpacer1,rightItem2];
}


- (void)buildNavRightItemWithImgName:(NSString *)imgName action:(SEL)action
{
    UIBarButtonItem *rightItem =[UIBarButtonItem itemWithImageName:imgName highImageName:imgName target:self action:action];
    
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    negativeSpacer.width = -10;
    
    self.navigationItem.rightBarButtonItems = @[negativeSpacer,rightItem];
}


- (void)buildNavRightItemWithImgName:(NSString *)imgName action:(SEL)action tag:(NSInteger)tag
{
    UIBarButtonItem *rightItem =[UIBarButtonItem itemWithImageName:imgName highImageName:imgName target:self action:action];
    UIButton *btn = (UIButton *)rightItem.customView;
    btn.tag = tag;
    
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    negativeSpacer.width = -10;
    
    self.navigationItem.rightBarButtonItems = @[negativeSpacer,rightItem];
}

- (void)buildNavLeftItemWithImgName:(NSString *)imgName action:(SEL)action
{
    UIBarButtonItem *leftItem =[UIBarButtonItem itemWithImageName:imgName highImageName:imgName target:self action:action];
    
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    negativeSpacer.width = -10;
    
    self.navigationItem.leftBarButtonItems = @[negativeSpacer,leftItem];
}

- (void)buildNavLeftItemWithTitle:(NSString *)title action:(SEL)action
{
    UIBarButtonItem *leftItem = [UIBarButtonItem addTarget:self WithTitle:title withColor:itemColor action:action];
    
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    negativeSpacer.width = -10;
    
    self.navigationItem.leftBarButtonItems = @[negativeSpacer,leftItem];
}


- (void)buildNavRightItemWithTitle:(NSString *)title action:(SEL)action
{
    UIBarButtonItem *rightItem =[UIBarButtonItem addTarget:self WithTitle:title withColor:itemColor action:action];
    
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    negativeSpacer.width = -10;
    
    self.navigationItem.rightBarButtonItems = @[negativeSpacer,rightItem];
}

- (void)buildNavBackItemWithAction:(SEL)action
{
//    [self buildNavLeftItemWithImgName:@"nav_back" action:action];
    [self barBackItemWithTarget:self sel:action];
}

/**
 *  创建导航返回按钮
 *
 *  @param sel 响应的方法
 */
- (void)barBackItemWithTarget:target sel:(SEL)sel
{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateHighlighted];
    button.zxl_size = button.currentImage.size;
    button.zxl_width = button.currentImage.size.width+15;
    [button setImageEdgeInsets:UIEdgeInsetsMake(0, -15, 0, 0)];
    [button addTarget:target action:sel forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    negativeSpacer.width = -10;
    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer,item, nil];
}



@end
